package xnj.lazydog.btcontroller.ControlViews.ControlViews;


import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import xnj.lazydog.btcontroller.ControlViews.Value;
import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.R;

public class Control2AxisPlot extends ControlElement {

    public Control2AxisPlot (ViewOptions options)
    {
        super(options);




    }
    float THUMB_RADIUS_RATIO = 0.05f;
    float thumb_radius_pix;
    float edge_pix_half;

    RectF mid_rect=new RectF();


    float w_range;
    float h_range;

    @Override
    public void onSizeChanged() {

        edge_pix = (w_pix+h_pix)*0.01f;

        if(edge_pix>max_edge_pix)
            edge_pix = max_edge_pix;

        if(edge_pix<min_edge_pix)
            edge_pix = min_edge_pix;

        p.setStrokeWidth(edge_pix);

        edge_pix_half = edge_pix*0.5f;

        thumb_radius_pix = (w_pix+h_pix)*THUMB_RADIUS_RATIO;

        w_range = w_pix-edge_pix;
        h_range = h_pix-edge_pix;
        rectF.right = w_pix*0.5f;
        rectF.left = -rectF.right;
        rectF.bottom = h_pix*0.5f;
        rectF.top = -rectF.bottom;

        back_rect.left = rectF.left+edge_pix_half;
        back_rect.right = rectF.right-edge_pix_half;
        back_rect.top = rectF.top+edge_pix_half;
        back_rect.bottom = rectF.bottom-edge_pix_half;

        mid_rect.left = rectF.left*0.5f;
        mid_rect.right = rectF.right*0.5f;
        mid_rect.top = rectF.top*0.5f;
        mid_rect.bottom = rectF.bottom*0.5f;


        if(options.on_en)
        {
            if(linkedValues[0]!=null)
                linkedValues[0].set(options.off_value+(options.on_value-options.off_value)*0.5f);
            if(linkedValues[1]!=null)
                linkedValues[1].set(options.off_value+(options.on_value-options.off_value)*0.5f);
        }

    }


    final int  BUFFER_SIZE = 1000;

    float [] pos_x = new float[BUFFER_SIZE];
    float [] pos_y = new float[BUFFER_SIZE];

    int buffer_index = 0;

    void updateBuffer(float x,float y)
    {
        buffer_index++;
        if(buffer_index>=BUFFER_SIZE)
          buffer_index -=BUFFER_SIZE;
        pos_x[buffer_index] = x;
        pos_y[buffer_index] = y;

    }

    Path getPath()
    {
        Path path = new Path();

        int index = buffer_index;

        if(index>=BUFFER_SIZE)
            index -=BUFFER_SIZE;


        if(options.sample_point_size>=1000)
            options.sample_point_size = 999;

        if(options.sample_point_size<=1)
            options.sample_point_size = 2;

        path.moveTo(pos_x[index],pos_y[index]);

        for(int i=0;i<options.sample_point_size;i++)
        {
            index--;
            if(index<0)
                index+=BUFFER_SIZE;
            path.lineTo(pos_x[index],pos_y[index]);
        }
        return path;
    }

    float ph=0;
    @Override
    public void draw(Canvas canvas) {

        p.setStyle(Paint.Style.FILL);
        p.setColor(THEME_GRAY);
        canvas.drawRect(back_rect,p);

        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(edge_pix*0.6f);

        //p.setColor(0xffc4c4c4);
        //canvas.drawRoundRect(mid_rect,edge_pix,edge_pix,p);

        p.setColor(0xffdddddd);
        canvas.drawLine(rectF.left, 0,rectF.right,0,p);
        canvas.drawLine(0, rectF.top,0,rectF.bottom,p);

        p.setStrokeWidth(edge_pix*0.75f);
        p.setColor(THEME_EDGE);
        canvas.drawPath(getPath(),p);

        p.setStrokeWidth(edge_pix);
        p.setColor(THEME_WHITE);
        canvas.drawRoundRect(rectF,edge_pix,edge_pix,p);


        p.setStyle(Paint.Style.FILL);
        p.setColor(THEME_EDGE);
        canvas.drawCircle(pos_x[buffer_index],pos_y[buffer_index],edge_pix,p);

    }



    @Override
    public void showSettingsDialog(Context context) {


        if(linkedValues[0]==null) {
            Toast.makeText(context,"请先链接一个变量",Toast.LENGTH_SHORT).show();
            return;
        }
        final EditText on_edit,off_edit;
        Button ok;
        TextView info;
        final EditText editText;
        String str;
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View layout = LayoutInflater.from(context).inflate(R.layout.toy_settings_xyplot,null);
        builder.setView(layout);
        dialog = builder.create();
        if(dialog.getWindow()!=null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        on_edit = layout.findViewById(R.id.on_edit);
        off_edit = layout.findViewById(R.id.off_edit);
        info = layout.findViewById(R.id.button_info);
        ok = layout.findViewById(R.id.button);
        editText = layout.findViewById(R.id.sample);



        if(options.sample_point_size<2)
            options.sample_point_size=2;

        str = ""+options.sample_point_size;

        editText.setText(str);

        if(linkedValues[0].type<4)
            str=(""+(int)(options.on_value));
        else
            str=(""+options.on_value);


        on_edit.setText(str);
        on_edit.setSelection(str.length());


        if(linkedValues[0].type<4)
            str=(""+(int)(options.off_value));
        else
            str=(""+options.off_value);


        off_edit.setText(str);
        off_edit.setSelection(str.length());


        if(linkedValues[0].type<4)
            str = "此控件链接的变量为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(int)(MIN_VALUES[linkedValues[0].type])+"~"+(int)(MAX_VALUES[linkedValues[0].type]);
        else
            str = "此控件链接的变量为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(MIN_VALUES[linkedValues[0].type])+"~"+(MAX_VALUES[linkedValues[0].type]);

        info.setText(str);

        initLimitedEdit(on_edit,info,linkedValues[0].type);
        initLimitedEdit(off_edit,info,linkedValues[0].type);

        initLimitedEdit(editText,2,1000);


        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Value value = getNumber(on_edit.getText().toString());
                if(value.type==0)
                {
                    options.on_value = 0;
                }else
                {
                    options.on_value = value.getFloat();
                }

                value = getNumber(off_edit.getText().toString());
                if(value.type==0)
                {
                    options.off_value = 0;

                }else
                {
                    options.off_value = value.getFloat();
                }

                options.threshold = (options.on_value+options.off_value)*0.5f;
                options.sample_point_size = getNumber(editText.getText().toString(),20);

                dialog.dismiss();
            }
        });
        dialog.show();
    }



    @Override
    public void clicked(Context context) {

        showSettingsDialog(context);
    }

    @Override
    public void dragged() {
    }
    @Override
    public void released() {
    }
    @Override
    public void pressed() {
    }
    @Override
    public void zoomed(float sx, float sy) {

    }

    @Override
    public void onRxData() {

        updateData();

    }

    public void updateData()
    {
        float pro_x,pro_y;

        if(linkedValues[0]==null)
            pro_x = 0.5f;
        else
            pro_x = (linkedValues[0].getFloat()-options.off_value)/(options.on_value-options.off_value);

        if(linkedValues[1]==null)
            pro_y = 0.5f;
        else
            pro_y = (linkedValues[1].getFloat()-options.off_value)/(options.on_value-options.off_value);


        if(pro_x>1)
            pro_x = 1;
        if(pro_x<0)
            pro_x = 0;

        if(pro_y>1)
            pro_y = 1;
        if(pro_y<0)
            pro_y = 0;

        updateBuffer((pro_x-0.5f)*w_range,(-pro_y+0.5f)*h_range);
    }

}
